﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// ZhGetData 的摘要说明：珠华平台数据请求处理类
    /// </summary>
    public class ZhGetData
    {
        public static string result(string MeterId)
        {
            string str_result = "";
            string url = System.Configuration.ConfigurationManager.AppSettings["ZhGetData_URL"].ToString();

            string str_timestamp = WebSdkCore.GetTimeStamp();

            string str_data = "";
            str_data = "meter_number=[\"" + MeterId + "\"]&timestamp=" + str_timestamp + "&secret=8e20f76567";

            string str_sign = "";
            str_sign = WebSdkCore.GetMD5String(str_data);

            string str_jsonParam = "{\"meter_number\":[\"" + MeterId + "\"],\"timestamp\":\"" + str_timestamp + "\",\"sign\":\"" + str_sign + "\"}";

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            byte[] byteData = Encoding.UTF8.GetBytes(str_jsonParam);

            int length = byteData.Length;
            request.ContentLength = length;
            Stream writer = request.GetRequestStream();
            writer.Write(byteData, 0, length);
            writer.Close();

            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();

            str_result = responseString.ToString();
            return str_result;
        }
    }
}